package cn.rongcloud.im.ui.moudle.manager.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import cn.rongcloud.im.R;
import cn.rongcloud.im.ui.activity.BaseActivity;
import cn.rongcloud.im.ui.bean.MoudleChildBean;
import cn.rongcloud.im.ui.view.ChannelTagView;

/**
 * Created by Administrator on 2017/3/24.
 */

public class ManagerActivity extends BaseActivity implements View.OnClickListener{
    /**ChannelView控件*/
    private ChannelTagView mChannelTagView;
    /**被添加的模块*/
    private ArrayList<MoudleChildBean> mAddedChannelsList;
    /**未被添加的模块*/
    private ArrayList<MoudleChildBean> mUnAddedChannels;
    /** 频道显示列数*/
    private int colums = 3;
    /**编辑按钮*/
    private Button mEditButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager);
        initParams();
        initData();
        initView();
        setView();
        setListener();
    }

    /**
     * 设置控件监听
     */
    private void setListener() {
        mEditButton.setOnClickListener(this);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mChannelTagView = (ChannelTagView) findViewById(R.id.channel_tag_view);
        mEditButton = getHeadRightButton();
    }

    /**
     * 设置控件
     */
    private void setView() {
        setTitle(getResources().getString(R.string.application_manager));
        mEditButton.setText(getResources().getString(R.string.edit));
        mChannelTagView.initChannels(mAddedChannelsList ,mUnAddedChannels );
    }

    /**
     * 初始化参数
     */
    private void initParams() {
        mAddedChannelsList = new ArrayList<>();
        mUnAddedChannels = new ArrayList<>();
    }

    /**
     * 初始化数据
     */
    private void initData() {
        addChannelItemToList("云对讲" ,0 , mAddedChannelsList ,true);
        addChannelItemToList("云停车" ,1 , mAddedChannelsList ,true);
        addChannelItemToList("云视频" ,2 , mAddedChannelsList ,true);
        addChannelItemToList("蓝牙摇摇" ,3 , mAddedChannelsList ,false);
        addChannelItemToList("扫一扫1" ,4, mAddedChannelsList,false);
        addChannelItemToList("扫一扫2" ,5, mAddedChannelsList,false);
        addChannelItemToList("扫一扫3" ,6, mAddedChannelsList,false);
        addChannelItemToList("扫一扫4" ,7, mAddedChannelsList,false);
        addChannelItemToList("扫一扫5" ,8, mAddedChannelsList,false);
        addChannelItemToList("扫一扫6" ,9, mAddedChannelsList,false);
        addChannelItemToList("扫一扫7" ,10, mAddedChannelsList,false);

        addChannelItemToList("信报箱" ,0, mUnAddedChannels,false);
        addChannelItemToList("智能家具" ,1 , mUnAddedChannels,false);
        addChannelItemToList("住宅报警" ,2 , mUnAddedChannels,false);
        addChannelItemToList("车辆管理" ,3 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请1" ,4 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请2" ,5 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请3" ,6 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请4" ,7 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请5" ,8 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请6" ,9 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请7" ,10 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请8" ,11 , mUnAddedChannels,false);
        addChannelItemToList("访客邀请9" ,12, mUnAddedChannels,false);
        addChannelItemToList("访客邀请10" ,13, mUnAddedChannels,false);
        addChannelItemToList("访客邀请11" ,14, mUnAddedChannels,false);

        fillData(mAddedChannelsList );
        fillData(mUnAddedChannels );
    }

    /**
     * 补全数据，因为使用到了RecycleView的GridLayout，每行需要3个，如果没到3个需要补充对应的空白数据
     * @param channels
     */
    private void fillData(ArrayList<MoudleChildBean> channels) {
        int lenth = channels.size();
        if(lenth % colums == 0){
            return;
        }
        int count = colums -(lenth % colums);
        MoudleChildBean item;
        for(int i= 0 ; i < count ; i ++){
            item = new MoudleChildBean();
            item.setMoudleChildName("");
            item.setSpace(true);
            channels.add(item);
        }
    }
    /**
     * 添加ChannelItem数据到List中
     * @param name
     * @param position
     * @param list
     */
    private void addChannelItemToList(String name, int position, ArrayList<MoudleChildBean> list
            , boolean notMove) {
        MoudleChildBean item = new MoudleChildBean();
        item.setNotMove(notMove);
        item.setPosition(position);
        item.setMoudleChildName(name);
        list.add(item);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_right:
                mChannelTagView.setEditorState((TextView) view);
        }
    }
}
